package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jx.dao.FindFuncByDidDao;
import com.jx.dao.FindFuncsDao;
import com.jx.daoimpl.FindFuncByDidDaoimpl;
import com.jx.daoimpl.FindFuncsDaoImpl;
import com.jx.model.Func;
import com.jx.service.FindFuncsService;
import com.jx.servicrimpl.FindFuncsServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 查询选中的部门的功能
 * @author zzzzzzz
 *
 */
public class FindCurrentFuncController extends HttpServlet{
	FindFuncByDidDao ffbdd=new FindFuncByDidDaoimpl();
	FindFuncsService ffs=new FindFuncsServiceImpl();
	FindFuncsDao ffd=new FindFuncsDaoImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		//获取选中部门的id
		int did=Integer.parseInt(req.getParameter("did"));
		List<Func> currentFunc=ffbdd.findFuncByDid(did);
		//获取所有的功能
		List<Func> allFuncs =ffd.findFuncsDao(0, ffd.findTotalFuncs());

		//循环比较
		for (Func funcs : allFuncs) {
			 for (Func func : currentFunc) {
				 if(func.getFname().equals(funcs.getFname())){
					 	funcs.setIschecked(1);
					 	break;
				 }
				 funcs.setIschecked(0);
			}
		}
//		写成json格式
		//{ischecked=1/0;func}
		JSONArray funcsarr=new JSONArray();
		JSONObject jfuncs=new JSONObject();
		for (Func func : allFuncs) {
			jfuncs.put("ischecked", func.getIschecked());
			jfuncs.put("func", func);
			funcsarr.add(jfuncs);
		}
		PrintWriter pw=resp.getWriter();
		pw.write(String.valueOf(funcsarr));
	}
}
